package com.dj.merchant.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import com.dj.merchant.mapper.MerchantInfoMapper;
import com.dj.merchant.mapper.MerchantModelRelationMapper;
import com.dj.merchant.model.MerchantInfo;
import com.dj.merchant.model.MerchantModelRelation;
import com.dj.merchant.model.MerchantParamList;
import com.dj.merchant.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantInfoMapper merchantInfoMapper;

    @Autowired
    private MerchantModelRelationMapper merchantModelRelationMapper;

    @Override
    public Object createMerchant(MerchantInfo merchantInfo) {
        if(merchantInfo.getMerchantName().length()>100){
            return "商户名称太长";
        }
        if(merchantInfo.getActiveStatus()==null){
            return "商户有效状态不能为空";
        }
        if(merchantInfo.getRegisterDate()==null){
            return "商户注册日期不能为空";
        }
        if(merchantInfo.getCreateTime()==null){
            merchantInfo.setCreateTime(this.getNow());
        }
        if(merchantInfo.getUpdateTime()==null){
            merchantInfo.setUpdateTime(this.getNow());
        }
        merchantInfoMapper.insertSelective(merchantInfo);
        //获取生成后的id
        Long merchantId = merchantInfo.getMerchantId();
        MerchantModelRelation merchantModelRelation = this.createMerchantModelRelationByMerchantInfo(merchantId, merchantInfo);
        List<Integer> businessModelType = merchantInfo.getBusinessModelType();
        for (int i = 0; i < businessModelType.size(); i++) {
            merchantModelRelation.setBusinessModelType(businessModelType.get(i));
            merchantModelRelationMapper.insertSelective(merchantModelRelation);
        }
        return "success";
    }

    @Override
    public MerchantInfo getMerchantById(Long merchantId) {
        MerchantInfo merchantInfo = merchantInfoMapper.selectByPrimaryKey(merchantId);
        if(merchantInfo==null){
            return new MerchantInfo();
        }
        List<Integer> businessModelType = merchantModelRelationMapper.getBusinessModelTypeById(merchantId);
        merchantInfo.setBusinessModelType(businessModelType);
        return merchantInfo;
    }

    @Override
    public List<MerchantInfo> queryBySearch(MerchantParamList merchantParamList) {
        int pageNum = merchantParamList.getPageNum();
        int pageSize = merchantParamList.getPageSize();
        int total;
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1 ) {
            pageSize = 10;
        }
        List<MerchantInfo> merchantInfos = merchantInfoMapper.queryBySearch(merchantParamList);
        List<MerchantInfo> MerchantInfoList = null;
        total = merchantInfos.size();
        MerchantInfoList = merchantInfos.subList(pageSize * (pageNum - 1), ((pageSize * pageNum) > total ? total : (pageSize * pageNum)));
        return MerchantInfoList;
    }

    @Override
    public Integer updateMerchantByExample(MerchantInfo merchantInfo) {
        int result = merchantInfoMapper.updateByPrimaryKeySelective(merchantInfo);
        Long merchantId = merchantInfo.getMerchantId();
        MerchantModelRelation merchantModelRelation = this.createMerchantModelRelationByMerchantInfo(merchantId, merchantInfo);
        List<Integer> businessModelType = merchantInfo.getBusinessModelType();
        //校验传来的商户合作模式是否为空,并把数据按照从小到大排序
        if (businessModelType == null) {

        }
        Collections.sort(businessModelType);
        //之前库中存储的合作模式.
        List<Integer> businessModelTypeById = merchantModelRelationMapper.getBusinessModelTypeById(merchantId);
        Collections.sort(businessModelTypeById);
        System.out.println(businessModelTypeById);
        if (businessModelType.size() == businessModelTypeById.size()) {
            for (int i = 0; i < businessModelType.size(); i++) {
                if (businessModelType.get(i) != businessModelTypeById.get(i)) {
                    int businessModelTypeUpdate = businessModelType.get(i);
                    int businessModelTypeOrigin = businessModelTypeById.get(i);
                    merchantModelRelationMapper.updateBusinessModelType(merchantId, businessModelTypeUpdate, businessModelTypeOrigin);
                }
            }
        }
        if (businessModelType.size() != businessModelTypeById.size()) {
            merchantModelRelationMapper.deleteBusinessModelType(merchantId);
            for (int i = 0; i < businessModelType.size(); i++) {
                merchantModelRelation.setBusinessModelType(businessModelType.get(i));
                merchantModelRelationMapper.insertSelective(merchantModelRelation);
            }
        }
        return result;
    }

    /**
     * 根据商户生成商户合作模式
     */
    private MerchantModelRelation createMerchantModelRelationByMerchantInfo(Long merchantId, MerchantInfo merchantInfo) {
        try {
            MerchantModelRelation merchantModelRelation = new MerchantModelRelation();
            merchantModelRelation.setMerchantId(merchantId);
            int activeStatus = (int) merchantInfo.getActiveStatus();
            if (activeStatus == 10) {
                merchantModelRelation.setActiveStatus(1);
            } else {
                merchantModelRelation.setActiveStatus(0);
            }
            merchantModelRelation.setCreateTime(merchantInfo.getCreateTime());
            merchantModelRelation.setCreateUser(merchantInfo.getCreateUser());
            merchantModelRelation.setUpdateTime(merchantInfo.getUpdateTime());
            merchantModelRelation.setUpdateUser(merchantInfo.getUpdateUser());
            return merchantModelRelation;
        } catch (Exception e) {
            e.printStackTrace();
            return new MerchantModelRelation();
        }
    }

    private Date getNow() {
        String formatDate = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        DateTime parse = DateUtil.parse(formatDate);
        return parse;
    }
}
